package cn.t.service.common;

import cn.t.constants.CommonConstants;
import cn.t.core.service.ServiceImpl;
import cn.t.enums.SyncStatusEnum;
import cn.t.model.common.MqProcessLogDO;
import cn.t.repository.common.MqProcessLogRepository;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * mq消息日志表领域服务
 */
@Slf4j
@Service
@AllArgsConstructor
public class MqProcessLogService extends ServiceImpl<MqProcessLogDO, MqProcessLogRepository> {

    public Map<String, MqProcessLogDO> getMqLogMap(List<String> businessKeys) {
        MqProcessLogDO queryParams = new MqProcessLogDO();
        queryParams.setBusinessKeys(businessKeys);

        List<MqProcessLogDO> mqProcessLogs = this.findByModel(queryParams, CommonConstants.ZERO, Integer.MAX_VALUE, "gmt_created", "asc");

        return mqProcessLogs.stream().collect(Collectors.toMap(MqProcessLogDO::getBusinessKey, Function.identity(), (a, b) -> b));
    }
    public void insertWorkOrderLog(String mqMsg,String businessKey,String  queue) {
        MqProcessLogDO logBean = new MqProcessLogDO();
        logBean.setProcessTime(new Date());
        logBean.setQueue(queue);
        logBean.setStatus(SyncStatusEnum.UNSYNC.getCode());
        logBean.setBusinessKey(businessKey);
        if (!ObjectUtils.isEmpty(mqMsg)){
            logBean.setMqContent(mqMsg);
        }
        repository.add(logBean);
    }
}
